#include "buildTransform.h"

#include <FnScenegraphIterator/FnScenegraphIterator.h>
#include <RenderOutputUtils/RenderOutputUtils.h>
#include <Render/RenderSettings.h>


Transform *getWorldToObjTransform( FnKat::Render::RenderSettings &renderSettings,
                                   FnKat::FnScenegraphIterator sgIt )
{
    vector<float>times;
    times.push_back(0.0f);

    FnKat::DoubleAttribute matrixAttr = FnKat::RenderOutputUtils::getMultiSampleWorldSpaceXForm(sgIt, &times);

    return new Transform(getMatrix4x4FromAttr(matrixAttr));
}


Matrix4x4 getMatrix4x4FromAttr(FnKat::DoubleAttribute attr, float time)
{
    if(!attr.isValid())
    {
        return Matrix4x4();
    }

    int pos = 0;
    FnKat::DoubleConstVector vec = attr.getNearestSample(time);
    float mat[4][4];

    for(int j = 0; j < 4; ++j)
    {
        for(int i = 0; i < 4; ++i)
        {
            mat[i][j] = vec[pos++];
        }
    }

    return Matrix4x4(mat);
}
